CQRS Pattern
1 min readRapid overview
CQRS Pattern
TL;DR
CQRS separates command (write) operations from query (read) operations to keep models focused.
Why it matters
- Separate cached read models from mutation flows.
- Keep optimistic updates and cache invalidation explicit.
- Align with data fetching libraries (React Query/SWR).
How it works
Example (TypeScript)
type Order = { id: string; status: string };
// Query model (read-optimized)
const fetchOrder = async (id: string): Promise<Order> => {
const res = await fetch(`/api/orders/${id}`);
return res.json();
};
// Command model (write-optimized)
const updateOrderStatus = async (id: string, status: string) => {
await fetch(`/api/orders/${id}/status`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status })
});
};
Quick recall Q&A
Q: When is CQRS overkill? A: Small apps where read/write separation adds complexity without clear benefit.